<?
/****************************************/
/************* IdeaLAN LLC **************/
/* This script is gonna calculate and   */
/* display page links, like next, prev  */
/* and count pages.                     */
/****************************************/
/* SCRIPT: pages.php          VER: 1.0  */
/* AUTHOR: Lukasz Piekarski             */
/* E-mail: lukasz@idealan.net           */
/****************************************/


//****************************************************
//
//   ATTACHE THIS TO THE PAGE, YOU WANT THE NEXT LINK AND PREVIOUS TO BE DISPLAYED
//
//  $result = mysql_query("SELECT * FROM `".$DBprefix."logs` WHERE `log_action` LIKE 'LOGIN' AND `log_userid` = '$info_user_id' ORDER BY `log_id`"); 
//  $rownumbers = mysql_num_rows($result);
//  $linktest ="in.php?show=profile&go=logs";
//
//  $link_prev || $pagejumpline || $link_next
//
// ***************************************************


// GET VARS FOR DISPLAYING FROM LINKS
$begin = $_GET["begin"];


// make sure that $begin is a number
if ($begin == ""){$begin=0;}
// make sure there is something to sort by
if ($sortby == ""){$sortby="username";}
// make sure there is a direction to search to
if ($direction == ""){$direction="ASC";}

// set the linkline variable to nothing
$linkline = "";
// check if 	there are more than 50 records
if (($rownumbers - $begin -20) > 20) {
	// add link to linkline
	$link_next = "$link_next || <a href=\"$linktest&begin=". ($begin + 20) ."\"> NEXT </a> ";
}else{
	// check it there are enough rows for another page, more than 0 is needed
	if (($rownumbers - $begin -20) > 0){
		// add the link generated with the number of rows to the linklink
		$link_next = "$link_next || <a href=\"$linktest&begin=". ($begin + 20) ."\"> NEXT </a> ";
	}
}
// check if this is a page after the first, if it is, make a link to go back 1 page
if ($begin > 0){$link_prev = "$link_prev <a href=\"$linktest&begin=". ($begin - 20) ."\">PREVIOUS</a> || ";}


	// set the page jump line to nothing
	$pagejumpline = "";
	// check if there would be more than 6 pages
	if ($rownumbers > 100){
	
		// write links to the first 3 pages. And check if the current page isn't one of them. If it is, write bold number.
		if ($begin != (20 * 1)-20 ) {$pagejumpline = "$pagejumpline <a href=\"$linktest&begin=". ((1*20)-20) ."\">1</a> ";}else{$pagejumpline = "$pagejumpline <u>1</u> ";}
		if ($begin != (20 * 2)-20 ) {$pagejumpline = "$pagejumpline <a href=\"$linktest&begin=". ((2*20)-20) ."\">2</a> ";}else{$pagejumpline = "$pagejumpline <u>2</u> ";}
		if ($begin != (20 * 3)-20 ) {$pagejumpline = "$pagejumpline <a href=\"$linktest&begin=". ((3*20)-20) ."\">3</a> ";}else{$pagejumpline = "$pagejumpline <u>3</u> ";}
		
		// check if it is needed to write numbers between (example: 1 2 3 ... 10 11 12 ... 98 99 100)
		if ($begin >= 120 && $begin < ($rownumbers-120)){
		
			// check it if is needed to write dots between the first 3 pages and the middel line
			// if the current page is 4 or 5 than it is not needed (example: 1 2 3 4 5 6 ... 98 99 100 after this page  1 2 3 ... 5 6 7 ... 98 99 100)
			// also check if there if there is needed for the 1ste number in the line. for example:
			// 1 2 3 4 5 ... 98 99 100 , in tis case the current page is 4, the page before 4 is 3 (doh) and 3 is already writen by default
			if ($begin != (20 * 4-20) ) {if ($begin != (20 * 5-20) ) {$pagejumpline = "$pagejumpline ... ";}
			$pagejumpline = "$pagejumpline <a href=\"$linktest&begin=". ($begin - 20) ."\">".((($begin+20)/20)-1)."</a> ";
			}
			// write the current page numer
			$pagejumpline = "$pagejumpline  ".(($begin+20)/20)." ";
			// check it there is needed for the 3de number in the middel line.
			if ($begin != (20 * (ceil($rownumbers/20)-3)-20) ) {$pagejumpline = "$pagejumpline <a href=\"$linktest&begin=". ($begin + 20) ."\">".((($begin+20)/20)+1)."</a> ";
			// check if there is neede for dots.
			if ($begin != (20 * (ceil($rownumbers/20)-4)-20) ) {$pagejumpline = "$pagejumpline ... ";}
		}
		// if there is no need for the middel line, place dots to seperate.
		}else{
			$pagejumpline = "$pagejumpline ..( ".(ceil($begin/20)+1)." ).. ";
		}
	
	// write the last 3 pages.
	if ($begin != (20 * (ceil($rownumbers/20)-2)-20) ) {$pagejumpline = "$pagejumpline <a href=\"$linktest&begin=". (((ceil($rownumbers/20)-2)*20)-20) ."\">".(ceil($rownumbers/20)-2)."</a> ";}else{$pagejumpline = "$pagejumpline <u>".(ceil($rownumbers/20)-2)."</u> ";}
	if ($begin != (20 * (ceil($rownumbers/20)-1)-20) ) {$pagejumpline = "$pagejumpline <a href=\"$linktest&begin=". (((ceil($rownumbers/20)-1)*20)-20) ."\">".(ceil($rownumbers/20)-1)."</a> ";}else{$pagejumpline = "$pagejumpline <u>".(ceil($rownumbers/20)-1)."</u> ";}
	if ($begin != (20 * (ceil($rownumbers/20)-0)-20) ) {$pagejumpline = "$pagejumpline <a href=\"$linktest&begin=". (((ceil($rownumbers/20)-0)*20)-20) ."\">".(ceil($rownumbers/20))."</a> ";}else{$pagejumpline = "$pagejumpline <u>".(ceil($rownumbers/20)-0)."</u> ";}

}
//**************************************************


?>